$(document).ready(function() {
	$(".top_nav").mousedown(function(e) {
		$(this).css("cursor", "move");// 改变鼠标指针的形状
		var offset = $(this).offset();// DIV在页面的位置
		var x = e.pageX - offset.left;// 获得鼠标指针离DIV元素左边界的距离
		var y = e.pageY - offset.top;// 获得鼠标指针离DIV元素上边界的距离
		$(document).bind("mousemove", function(ev) { // 绑定鼠标的移动事件，因为光标在DIV元素外面也要有效果，所以要用doucment的事件，而不用DIV元素的事件

			$(".popup").stop();// 加上这个之后

			var _x = ev.pageX - x;// 获得X轴方向移动的值
			var _y = ev.pageY - y;// 获得Y轴方向移动的值

			$(".popup").animate({
				left : _x + "px",
				top : _y + "px"
			}, 10);
		});

	});

	$(document).mouseup(function() {
		$(".popup").css("cursor", "default");
		$(this).unbind("mousemove");
	});

	// 窗口效果
	// 点击登录class为tc 显示
	$(".tc").click(function() {
		$.ajax({
			type : "post",
			url : appPath + "/address/add.htm",
			data : {
				token : $.cookie("token")
			},
			dataType : "html",
			success : function(result) {
				if(result.indexOf("loginpage") > -1){
					location.href = appPath + "/member/login.htm";
				}
				$("#popup1").html(result);
				$("#cover").show();
				$("#popup1").show();// 查找ID为popup的DIV show()显示#cover
				tc_center_center("popup1");
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				myalert("系统异常，请稍后再试");
			}
		});
	});

	// 窗口水平居中
	$(window).resize(function() {
		tc_center_center();
	});
})

function tc_center_center(id) {
	var _top = (screen.height - $("#" + id).height()) / 2 + $(window).scrollTop() - 85;
	var _left = (screen.width - $("#" + id).width()) / 2;
	$("#" + id).css({
		top : _top,
		left : _left
	});
}

function del(addressid) {
	myconfirm("确定要删除吗？", function() {
		showLoading();
		$.ajax({
			type : "post",
			url : appPath + "/address/del.html",
			data : {
				addressid : addressid,
				token : $.cookie("token")
			},
			success : function(result) {
				hideLoading();
				myalert(result.msg, function() {
					if (result.code == '1') {
						location.reload();
					} else if (result.code == -1) {
						window.location = appPath + "/member/login.htm";
					}
				});
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				hideLoading();
				myalert("系统异常，请稍后再试");
			}
		});
	})
}

function setDefault(addressid) {
	showLoading();
	$.ajax({
		type : "post",
		url : appPath + "/address/setDefault.html",
		data : {
			addressid : addressid,
			token : $.cookie("token")
		},
		success : function(result) {
			hideLoading();
			myalert(result.msg, function() {
				if (result.code == '1') {
					location.reload();
				} else if (result.code == -1) {
					window.location = appPath + "/member/login.htm";
				}
			});
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			hideLoading();
			myalert("系统异常，请稍后再试");
		}
	});
}

function edit(addressId){
	$.ajax({
		type : "post",
		url : appPath + "/address/edit.htm",
		data : {
			addressid : addressId,
			token : $.cookie("token")
		},
		dataType : "html",
		success : function(result) {
			if(result.indexOf("loginpage") > -1){
				location.href = appPath + "/member/login.htm";
			}
			$("#popup1").html(result);
			$("#cover").show();
			$("#popup1").show();// 查找ID为popup的DIV show()显示#cover
			tc_center_center("popup1");
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			myalert("系统异常，请稍后再试");
		}
	});
}

function chooseOver(obj) {
	gotoHtml("/order/preOrderInfo.html?addressId=" + obj.id + "&singleBuyFlag=" + $("#singleBuyFlag").val());
}

var ohtml = "<option value='0' selected='selected'>请选择</option>";
var url = appPath + "/area/getAreas.html";
// 加载省份
function loadProvince(callback) {
	$.ajax({
		type : "POST",
		dataType : "json",
		url : url,
		data : {
			parentId : 0,
			token : $.cookie("token")
		},
		cache : false,
		success : function(data) {
			if (data) {
				var provinceHtml = ohtml;
				var result = eval('(' + data + ')');
				for (var i = 0; i < result.length; i++) {
					var area = result[i];
					provinceHtml += "<option value='" + area.id + "'>" + area.name + "</option>";
				}
				$("#provinceId").html(provinceHtml);
				if (callback) {
					callback();
				}
			}
		},
		error : function(XMLHttpResponse) {
		}
	});
}

function loadCity(callback) {
	var proEl = $("#provinceId option:selected");
	var provinceId = proEl.val();
	var provinceName = proEl.text();
	if (provinceId > 0) {
		$.ajax({
			type : "post",
			dataType : "json",
			url : url,
			data : {
				parentId : provinceId,
				token : $.cookie("token")
			},
			cache : false,
			success : function(data) {
				var cityHtml = ohtml;
				if (data) {
					var result = eval('(' + data + ')');
					for (var i = 0; i < result.length; i++) {
						var area = result[i];
						cityHtml += "<option value='" + area.id + "'>" + area.name + "</option>";
					}
				}
				$("#cityId").html(cityHtml);
				$("#countyId").html(ohtml);
				$("#area").val(provinceName);
				if (callback) {
					callback();
				}
			},
			error : function(XMLHttpResponse) {
			}
		});
	} else {
		$("#cityId").html(ohtml);
		$("#countyId").html(ohtml);
		$("#area").val("");
	}
}

function loadCounty(callback) {
	var cityEl = $("#cityId option:selected");
	var cityId = cityEl.val();
	var provinceName = $("#provinceId option:selected").text();
	var cityName = cityEl.text();
	if (cityId > 0) {
		$.ajax({
			type : "post",
			dataType : "json",
			url : url,
			data : {
				parentId : cityId,
				token : $.cookie("token")
			},
			cache : false,
			success : function(data) {
				if (data) {
					var countyHtml = ohtml;
					var result = eval('(' + data + ')');
					for (var i = 0; i < result.length; i++) {
						var area = result[i];
						countyHtml += "<option value='" + area.id + "'>" + area.name + "</option>";
					}
					$("#countyId").html(countyHtml);
					$("#area").val(provinceName + " " + cityName);
					if (callback) {
						callback();
					}
				}
			},
			error : function(XMLHttpResponse) {
			}
		});
	} else {
		$("#countyId").html(ohtml);
		$("#area").val("");
	}
}

function loadArea() {
	var province = $("#provinceId option:selected").val();
	var provinceName = $("#provinceId option:selected").text();
	if(province != '0'){
		$("#area").val(provinceName);
		var city = $("#cityId option:selected").val();
		var cityName = $("#cityId option:selected").text();
		if(city != '0'){
			$("#area").val(provinceName + " " + cityName);
			var county = $("#countyId option:selected").val();
			var countyName = $("#countyId option:selected").text();
			if(county != '0'){
				$("#area").val(provinceName + " " + cityName + " " + countyName);				
			}
		}
	}
}